﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YiSha.Business.Business;
using YiSha.Entity.Business;
using YiSha.Model.Param.Business;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Admin.Web.Controllers
{
    public class PersonController : BaseController
    {

        private PersonBLL personBLL = new PersonBLL();

        #region 视图功能


        [AuthorizeFilter("business:person:view")]
        public IActionResult PersonIndex()
        {
            return View();
        }


        public IActionResult PersonForm()
        {
            return View();
        }

        #endregion
                       
        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("business:person:search,business:person:view")]
        public async Task<IActionResult> GetListJson(PersonListParam param)
        {
            TData<List<PersonEntity>> obj = await personBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("business:person:search,business:person:view")]
        public async Task<IActionResult> GetPageListJson(PersonListParam param, Pagination pagination)
        {
            TData<List<PersonEntity>> obj = await personBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(long id)
        {
            TData<PersonEntity> obj = await personBLL.GetEntity(id);
            return Json(obj);
        }


        /// <summary>
        /// 根据处方id获取用户信息
        /// </summary>
        /// <param name="id">prescriptionId</param>
        /// <returns></returns>
        public async Task<IActionResult> GetprescriptionFormJsonFormJson(long id)
        {
            TData<PersonEntity> obj = await personBLL.GetEntityByPrescriptionId(id);
            return Json(obj);
        }



        [HttpGet]
        public async Task<IActionResult> GetPersonName(PersonListParam param)
        {
            TData<string> obj = new TData<string>();
            var list = await personBLL.GetList(param);
            if (list.Tag == 1)
            {
                obj.Data = string.Join(",", list.Data.Select(p => p.RealName));
                obj.Tag = 1;
            }
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("business:person:add,business:person:edit")]
        public async Task<IActionResult> SaveFormJson(PersonEntity entity)
        {
            entity.RealNameEn = entity.RealName.GetFirstLetters();
            TData<string> obj = await personBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("business:person:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TData obj = await personBLL.DeleteForm(ids);
            return Json(obj);
        }
        #endregion

    }
}
